const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 评分
    starCheckedImgUrl: '../../images/star1.png',
    starUnCheckedImgUrl: '../../images/star3.png',
    starMap: [
      '非常差',
      '差',
      '一般',
      '好',
      '非常好',
    ],
    goods_start: 5,
    shipping_start: 5,
    spee_start: 5,
    user_start: 5,
    pics: '',
    content: '',
    //上传凭证
    selectImageList: [],
    isShow: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options && options.order_sn != '') {
      console.log(options)
      this.setData({
        order_sn: options.order_sn
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this,
    login_status = wx.getStorageSync('login_status');
  if (!login_status || login_status <= 0) {
    wx.showLoading({
      title: '请先进行登陆',
    })
    setTimeout(() => {
      wx.hideLoading({
        success: (res) => {
          wx.navigateTo({
            url: '/pages/grant/grant',
          })
        },
      })
    }, 1500);
  }
  },
  // 产品评分
  chooseGoodsStart: function (obj) {
    var goods_start = obj.currentTarget.dataset.star;
    this.setData({
      goods_start: goods_start
    })
  },
  chooseShippingStart: function (obj) {
    var shipping_start = obj.currentTarget.dataset.star;
    this.setData({
      shipping_start: shipping_start
    })
  },
  chosseSpeeStart: function (obj) {
    var spee_start = obj.currentTarget.dataset.star;
    this.setData({
      spee_start: spee_start
    })
  },
  chooseUserStart: function (obj) {
    var user_start = obj.currentTarget.dataset.star;
    this.setData({
      user_start: user_start
    })
  },

  //点击上传图片
  selectBottom(e) {
    var that = this;
    const selectNum = that.data.selectImageList.length;
    const num = 6 - selectNum;
    num != 0 && wx.chooseImage({
      count: num,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: (res) => {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        let newList = that.data.selectImageList;
        for (let i = 0; i < tempFilePaths.length; i++) {
          newList.push(tempFilePaths[i]);
        }
        newList.length == 6 ? that.setData({
          isShow: false
        }) : null;
        that.setData({
          selectImageList: newList
        }, () => {
          // console.log(that.data.selectImageList)
        })
        // console.log("tempFilePaths:"+JSON.stringify(tempFilePaths))
      }
    })
  },
  //删除
  closeOption(e) {
    const {
      index
    } = e.currentTarget.dataset;
    let imagelist = this.data.selectImageList;
    imagelist.splice(index, 1);
    this.setData({
      selectImageList: imagelist,
      isShow: true
    })
  },

  //执行上传
  submit: function (obj) {
    var that = this,
      content = obj.detail.value['content'],
      goods_start = this.data.goods_start,
      shipping_start = this.data.shipping_start,
      spee_start = this.data.spee_start,
      user_start = this.data.user_start,
      newList = this.data.selectImageList;
    if (content == '') {
      wx.showToast({
        title: '请填写您的感受',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (newList.length > 0) {
      that.setData({
        content: content
      })
      that.uploadPic({
        path: newList
      });
    } else {
      that.setData({
        content: content
      })
      that.formsubimt();
    }
  },

  uploadPic: function (data) {
    var pics = this.data.pics ? this.data.pics : '';
    var that = this;
    var i = data.i ? data.i : 0; //当前上传的哪张图片
    var success = data.success ? data.success : 0; //上传成功的个数
    var fail = data.fail ? data.fail : 0; //上传失败的个数
    var photo_urls = this.data.photo_urls ? this.data.photo_urls : [];
    wx.uploadFile({
      url: https.url + '/upload',
      filePath: data.path[i],
      name: 'file', //这里根据自己的实际情况改
      formData: {
        id: data.id
      }, //这里是上传图片时一起上传的数据
      success: (resp) => {
        if (resp.data != '') {
          success++; //图片上传成功，图片上传成功的变量+1
          wx.showLoading({
            title: '上传第' + success + '张完成',
          })
          if (pics != '') {
            pics = pics + ',' + resp.data
          } else {
            pics = resp.data
          }
        } else {
          fail++
        }
        that.setData({
          pics: pics,
          imgs: [],
        })
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
      },
      complete: () => {
        i++; //这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) { //当图片传完时，停止调用          
          wx.hideLoading();
          wx.showToast({
            title: '上传完成',
            icon: 'success',
            duration: 1000
          });
          setTimeout(function () {
            that.formsubimt();
          }, 1000);

        } else { //若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadPic(data);
        }
      }
    });
  },

  formsubimt: function (obj) {
    var that = this,
      goods_start = this.data.goods_start,
      shipping_start = this.data.shipping_start,
      spee_start = this.data.shipping_start,
      user_start = this.data.user_start,
      content = this.data.content,
      user_id = wx.getStorageSync('user_id'),
      order_sn = this.data.order_sn,
      pics = this.data.pics;
    https.post('saveCommon', {
      goods_start: goods_start,
      shipping_start: shipping_start,
      spee_start: spee_start,
      user_start: user_start,
      content: content,
      user_id: user_id,
      order_sn: order_sn,
      pics: pics
    }, false, '提交评价').then(res => {
      if(res){
        wx.navigateBack({
          delta: 0,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

})